/**
 * Created by zhang on 2016/12/14.
 */
var visitorCount = {
    events:{
        "#visitorCountBody .radioCheck click" : "radioCheck",
        "#visitorCountBody .search-count click" : "searchCount",
        "#visitorCountBody .count-import click" : "countImport",
    },
    init: function () {
        var _this = this;
        document.title = "访客统计 - 访客管理";
        var temp = Util.template($("#visitorCount-template").html(),{});
        $(".commonBody-rightBody").html(temp);
        Util.event.call(_this,_this.events);
        _this.datetime();
        $(".radioCheck:eq(0)").click();
        _this.companyCountEcharts();
    },
    datetime : function(){
        $(".date-from").datetimepicker({
            format: 'yyyy-mm-dd',
            autoclose: true,
            language: "zh-CN",
            minView :2,
            pickerPosition: "bottom-left"
        });
    },
    radioCheck: function (ele) {
        var that = this,
            $thisRadio = $(ele).find(".user-radio-z");
        if ($(ele).find(".checked").length < 1) {
            $(".radioCheck .user-radio-z").removeClass("checked");
            $thisRadio.addClass("checked");
            var type = $thisRadio.attr("data-type"),start = "",end = "";
            if(type == "0"){start = new Date().format("yyyy-MM-dd");end = new Date().format("yyyy-MM-dd");}
            if(type == "1"){
                start = new Date(that.getWeekStartDate()).format("yyyy-MM-dd");
                end = new Date(that.getWeekEndDate()).format("yyyy-MM-dd");
            }
            if(type == "2"){
                start = new Date(that.getMonthStartDate()).format("yyyy-MM-dd");
                end = new Date(that.getMonthEndDate()).format("yyyy-MM-dd");
            }
            $("#openStart").val(start);
            $("#openEnd").val(end);
        }
    },
    settings:{
        donutSet:{
            tooltip : {
                trigger: 'axis',
                axisPointer : {            // 坐标轴指示器，坐标轴触发有效
                    type : 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
                }
            },
            grid: {
                top:'30',
                left: '30',
                right: '60',
                bottom: '60',
                containLabel: true
            },
            series: [
                {
                    type:'bar',
                    splitNumber: 5,
                    barWidth: '38',
                    xAxis : [{type : 'category'}],
                    yAxis : [{type : 'value'}],
                    data:[]
                }
            ]
        }
    },
    searchCount : function(){
        this.companyCountEcharts();
    },
    companyCountEcharts : function(){
        var _this = this;
        _this.type = $(".radioCheck .checked").attr("data-type");
        _this.starttime = $("#openStart").val();
        _this.endtime = $("#openEnd").val();
        Util.ajaxRequest({
            type: "get",
            url: interfaceURL.getVisitorCount(),
            dataType: "json",
            showLoading : false,
            async : false,
            timeout:60000,
            data:{
                endtime:_this.endtime,
                starttime : _this.starttime,
                type : _this.type
            },
            success : function(data){
                if(data.code == 200){
                    $(".vc-ci .vc-num").html(data.result.recordCount);
                    $(".vc-ren .vc-num").html(data.result.visitorCount);
                    _this.initCharts(data.result);
                }
            }
        })
    },
    initCharts :function(data){
        var _this = this;
        var myChart1 = echarts.init($(".companyCount").find(".vc-chart-box")[0]);
        var option1 = $.extend(true,{},_this.settings.donutSet,{
            color: ['#f98426'],
            xAxis : [
                {
                    name:'公司名',
                    data : data.companyCount.key,
                    axisLabel:{interval:0,rotate:30,textStyle:{fontSize:12}}
                }
            ],
            yAxis : [{name:'访问次数'}],
            series : [
                {
                    name:'次数',
                    data:data.companyCount.value
                }
            ]
        });
        myChart1.setOption(option1);

        var myChart2 = echarts.init($(".departmentCount").find(".vc-chart-box")[0]);
        var option2 = $.extend(true,{},_this.settings.donutSet,{
            color: ['#f98426'],
            xAxis : [
                {
                    name:'部门',
                    data : data.departCount.key,
                    axisLabel:{interval:0,rotate:30,textStyle:{fontSize:12}}
                }
            ],
            yAxis : [{name:'访问次数'}],
            series : [
                {
                    name:'次数',
                    data:data.departCount.value
                }
            ]
        });
        myChart2.setOption(option2);
    },
    countImport : function(){
        var num = $(".vc-ci .vc-num").html();
        if (num > 0) {
            var para = "?endtime="+this.endtime+"&starttime="+this.starttime+"&type="+this.type+"";
            window.open(interfaceURL.exportCount(para));
        }else{
            pop.mAlert("暂无访问!");
        }
    },
    /* 时间 change*/
    getCur : function(){
        return{
            nowDayOfWeek : new Date().getDay(), //今天本周的第几天 
            nowDay : new Date().getDate(), //当前日 
            nowMonth : new Date().getMonth(), //当前月 
            nowYear : new Date().getFullYear() //当前年 
        }
    },
    getWeekStartDate : function() {
        var cur =  this.getCur();
        var weekStartDate = new Date(cur.nowYear, cur.nowMonth, cur.nowDay - cur.nowDayOfWeek + 1); 
        return weekStartDate; 
    },
    getWeekEndDate : function() { 
        var cur =  this.getCur();
        var weekEndDate = new Date(cur.nowYear, cur.nowMonth, cur.nowDay + (7 - cur.nowDayOfWeek)); 
        return weekEndDate; 
    },
    getMonthStartDate : function(){
        var cur =  this.getCur();
        var monthStartDate = new Date(cur.nowYear, cur.nowMonth, 1); 
        return monthStartDate; 
    },
    getMonthEndDate : function(){
    var cur =  this.getCur(); 
        var monthEndDate = new Date(cur.nowYear, cur.nowMonth, this.getMonthDays(cur.nowMonth)); 
        return monthEndDate; 
    },
    getMonthDays : function(myMonth){ 
        var cur =  this.getCur();
        var monthStartDate = new Date(cur.nowYear, myMonth, 1); 
        var monthEndDate = new Date(cur.nowYear, myMonth + 1, 1); 
        var days = (monthEndDate - monthStartDate)/(1000 * 60 * 60 * 24); 
        return days; 
    }
};